package com.ai.notices.web.controller.busi;

import com.ai.notices.web.system.AppBaseController;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;

/**
 * Created by huangzh on 17/6/17.
 */
@Controller
@RequestMapping(value = "/main")
public class MainController extends AppBaseController {
    private static final Logger log = Logger.getLogger(MainController.class);

    /*
    * 功能描述:进入登录页面
    * 开发人员:huangzh5
    * */
    @RequestMapping(value="/pageInit")
    public String init() throws Exception{
        return "/index"; //返回页面
    }

    @RequestMapping(value = "/downloadExcel")
    public void downloadExcel(HttpServletResponse response, HttpServletRequest request) {
        try {
            //获取文件的路径
            String fileName = request.getParameter("fileName");
            String excelPath = request.getSession().getServletContext().getRealPath("/templete/"+fileName);
            // 读到流中
            InputStream inStream = new FileInputStream(excelPath);//文件的存放路径
            // 设置输出的格式
            response.reset();
            response.setContentType("bin");
            response.addHeader("Content-Disposition",
                    "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8"));
            // 循环取出流中的数据
            byte[] b = new byte[200];
            int len;

            while ((len = inStream.read(b)) > 0){
                response.getOutputStream().write(b, 0, len);
            }
            inStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
